package com.jade.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Format {
	/***formatContent***/
	public static String formatContent(String content) {
		if(content!=null && !"".equals(content)){
			//数据抓取的图片处理
			Pattern pattern = Pattern.compile("\\{#UPIMG\\}\\(.*?\\)\\{/#UPIMG\\}", Pattern.DOTALL| Pattern.CASE_INSENSITIVE);
			Matcher matcher = pattern.matcher(content);
			while (matcher.find()) {
				content = processUpimgFormat(content,matcher.group().trim());
			}
			//清除HTML格式得无格式纯文本
			pattern = Pattern.compile("\\{#CHTML\\}\\(.*?\\)\\{/#CHTML\\}", Pattern.DOTALL| Pattern.CASE_INSENSITIVE);
			matcher = pattern.matcher(content);
			while (matcher.find()) {
				content=processHtmlFormat(content,matcher.group().trim());
			}
			//将有格式文本转化为HTML
			pattern = Pattern.compile("\\{#TOHTML\\}\\(.*?\\)\\{/#TOHTML\\}", Pattern.DOTALL| Pattern.CASE_INSENSITIVE);
			matcher = pattern.matcher(content);
			while (matcher.find()) {
				content=processToHtmlFormat(content,matcher.group().trim());
			}
			//字符替换
			pattern = Pattern.compile("\\{#RE\\(.*?;[^;]*?\\)}\\(.*?\\)\\{/#RE\\}", Pattern.DOTALL| Pattern.CASE_INSENSITIVE);
			matcher = pattern.matcher(content);
			while (matcher.find()) {
				content=replaceFormat(content,matcher.group().trim());
			}
			//时间、长度截取、数字小数位，格式化
			pattern = Pattern.compile("#F\\(.*?,[^,]*?\\)", Pattern.DOTALL| Pattern.CASE_INSENSITIVE);
			matcher = pattern.matcher(content);
			while (matcher.find()) {
				content=processFormat(content,matcher.group().trim());
			}
			//字符截取
			pattern = Pattern.compile("#SUB\\(.*?,[^,]*?,[^,]*?\\)", Pattern.DOTALL| Pattern.CASE_INSENSITIVE);
			matcher = pattern.matcher(content);
			while (matcher.find()) {
				content=processSubFormat(content,matcher.group().trim());
			}
			//枚举
			pattern = Pattern.compile("#OPT\\(.*?;[^;]*?\\)", Pattern.DOTALL| Pattern.CASE_INSENSITIVE);
			matcher = pattern.matcher(content);
			while (matcher.find()) {
				content=processOptionFormat(content,matcher.group().trim(),matcher.group().trim().lastIndexOf(";"));
			}
			//数据切割拼接
			pattern = Pattern.compile("#HIDE\\(.*?;[^;]*?;[^;]*?;[^;]*?\\)", Pattern.DOTALL| Pattern.CASE_INSENSITIVE);
			matcher = pattern.matcher(content);
			while (matcher.find()) {
				content=processHideFormat(content,matcher.group().trim());
			}
			
		}else{
			content = "";
		}
		return content;
	}
	private static String replaceFormat(String content,String formatTag){
		//\\{#RE\\(.*?;[^;]*?\\)}\\(.*?\\)\\{/#RE\\}
		String formatStr = formatTag.substring(formatTag.indexOf(")}(")+3,formatTag.length()-7);
		String re = formatTag.substring(5,formatTag.indexOf(")}("));
		formatStr = formatStr.replaceAll("\\Q"+re.split(";")[0]+"\\E", CqUtil.quoteReplacement(re.split(";")[1]));
		content = content.replaceAll("\\Q"+formatTag+"\\E", CqUtil.quoteReplacement(formatStr));
		return content;
	}
	private static String processUpimgFormat(String content,String formatTag){
		String formatStr = formatTag.substring(9,formatTag.length()-10);
		formatStr = formatStr.replaceAll("\\Q{[img]}\\E", CqUtil.quoteReplacement("<img src=\""));
		formatStr = formatStr.replaceAll("\\Q{[/img]}\\E", CqUtil.quoteReplacement("\" style=\"width:90%;\" />"));
		content = content.replaceAll("\\Q"+formatTag+"\\E", CqUtil.quoteReplacement(formatStr));
		return content;
	}
	
	private static String processHtmlFormat(String content,String formatTag){
		String formatStr = formatTag.substring(9,formatTag.length()-10);
		formatStr = CqUtil.clearHTML(formatStr);
		content = content.replaceAll("\\Q"+formatTag+"\\E", CqUtil.quoteReplacement(formatStr));
		return content;
	} 
	private static String processToHtmlFormat(String content,String formatTag){
		String formatStr = formatTag.substring(10,formatTag.length()-11);
		formatStr = CqUtil.toHTML(formatStr);
		content = content.replaceAll("\\Q"+formatTag+"\\E", CqUtil.quoteReplacement(formatStr));
		return content;
	}
	private static String processSubFormat(String content,String formatTag){
		//#SUB(@@,0,end)
		String end = formatTag.substring(formatTag.lastIndexOf(",")+1,formatTag.length()-1);
		String formatStr = formatTag.substring(0,formatTag.lastIndexOf(","));
		String begin = formatStr.substring(formatStr.lastIndexOf(",")+1);
		formatStr = formatTag.substring(5,formatTag.indexOf(","));
		if("end".equals(end)){
			formatStr = formatStr.substring(Integer.parseInt(begin));
		}else if(end.indexOf("-")!=-1){
			formatStr = formatStr.substring(Integer.parseInt(begin),formatStr.length()-Integer.parseInt(end.split("-")[1]));
		}else{
			formatStr = formatStr.substring(Integer.parseInt(begin),Integer.parseInt(end));
		}
		content = content.replaceAll("\\Q"+formatTag+"\\E", CqUtil.quoteReplacement(formatStr));
		return content;
	}
	private static String processFormat(String content,String formatTag){
		int pos=formatTag.lastIndexOf(",");
		if(pos!=-1){
			String signStr=formatTag.substring(pos+1,formatTag.length()-1);
			if(signStr.indexOf("-")!=-1 || signStr.indexOf(":")!=-1 ){//时间处理 #F(@@,yyyy-MM-dd HH:mm:ss)
				content=processDateFormat(content,formatTag,pos);
			}else if (signStr.indexOf(".")==-1){//字段长度控制 #F(@@,100)
				content=processFieldFormat(content,formatTag,pos);
			}else if (signStr.indexOf(".")!=-1){//小数位处理 #F(@@,0.2)
				content=processDataFormat(content,formatTag,pos);
			}
		}
		return content;
	}
	private static String processOptionFormat(String content,String formatTag,int pos){
		String formatText=formatTag.substring(5,pos);
		String splitStr = "=";
		if(formatTag.indexOf("==")!=-1)
			splitStr = "==";
		String Text = "";
		String[] format = formatTag.substring(pos+1,formatTag.length()-1).split("#");
		for(int i=0;i<format.length;i++){
			if(formatText.equals(format[i].split(splitStr)[0])){
				Text = format[i].split(splitStr)[1];
			}
		}
		if("".equals(Text)){
			for(int i=0;i<format.length;i++){
				if("else".equals(format[i].split(splitStr)[0])){
					Text = format[i].split(splitStr)[1];
				}
			}
		}
		content=content.replaceAll("\\Q"+formatTag+"\\E", Text);
		return content;
	}
	private static String processHideFormat(String content,String formatTag){
		//#HIDE(数据;2;3;***)
		String formattext = formatTag;
		String displace = formattext.substring(formattext.lastIndexOf(";")+1,formattext.length()-1);
		formattext = formattext.substring(0,formattext.lastIndexOf(";"));
		String end = formattext.substring(formattext.lastIndexOf(";")+1,formattext.length());
		formattext = formattext.substring(0,formattext.lastIndexOf(";"));
		String begin = formattext.substring(formattext.lastIndexOf(";")+1,formattext.length());
		formattext = formattext.substring(0,formattext.lastIndexOf(";"));
		String Text = formattext.substring(formattext.lastIndexOf("(")+1,formattext.length());
		//切割Text
		int beginint = Integer.valueOf(begin).intValue();
		int endint = Integer.valueOf(end).intValue();
		if(Text.length()>(beginint+endint)){
			Text = Text.substring(0,beginint)+displace+Text.substring((Text.length()-endint));
		}else{
			Text=displace;
		}
		content=content.replaceAll("\\Q"+formatTag+"\\E", Text);
		return content;
	}
	private static String processDateFormat(String content,String formatTag,int pos){
		String formatText=formatTag.substring(3,pos);
		String format=formatTag.substring(pos+1,formatTag.length()-1);
		Date date=new Date();
		SimpleDateFormat sm = new SimpleDateFormat(format);
		try{
			if(!"".equals(formatText)){
				date=sm.parse(formatText);
				content=content.replaceAll("\\Q"+formatTag+"\\E", sm.format(date));
			}else{
				content=content.replaceAll("\\Q"+formatTag+"\\E", "");
			}
		}catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return content;
	}

	private static String processFieldFormat(String content,String formatTag,int pos){
		String formatText=formatTag.substring(3,pos);
		String format=formatTag.substring(pos+1,formatTag.length()-1);
		if(format.matches("\\d+")){
			int filedLength=Integer.parseInt(format);
			if(formatText.length()>filedLength)
				 formatText = formatText.substring(0,filedLength)+"...";
		}
		content=content.replaceAll("\\Q"+formatTag+"\\E", CqUtil.quoteReplacement(formatText));
		return content;
	}

	private static String processDataFormat(String content,String formatTag,int pos){
		String formatText=formatTag.substring(3,pos);
		String temformat=formatTag.substring(pos+1,formatTag.length()-1);
		
		int sign=temformat.indexOf(".");
		if(sign==-1){
			return content.replaceAll("\\Q"+formatTag+"\\E", formatText);
		}
		
		//小数格式化小数长度位
		String format=temformat.substring(sign+1);
		
		if(format.matches("\\d+")){
			int filedLength=Integer.parseInt(format);
			int point=formatText.indexOf(".");
			if(point!=-1&&formatText.substring(point+1).length()>filedLength){
				content=content.replaceAll("\\Q"+formatTag+"\\E", formatText.substring(0,point+1+filedLength));
			}else{
				content=content.replaceAll("\\Q"+formatTag+"\\E", formatText);
			}
		}else {
			content=content.replaceAll("\\Q"+formatTag+"\\E", formatText);
		}
		return content;
	}
	/***formatContent***/
}